﻿#include "bullet.h"



Bullets::Bullets()
{

}

Bullets::Bullets(QWidget *parent) : QWidget(parent){
    setFixedSize(1100, 980);
}

void Bullets::Bullet_fire(tank &t1,tank &t2){
    if(t1.isLive==1){
        if(clock()-t1.shoot_time>=300){
            if(GetAsyncKeyState('Q')&0x8000){
                t1.shoot_time=clock();
                if(b1.size()<8){
                    Bullet temp={
                        t1.x,
                        t1.y,
                        t1.angle+90,
                        10000
                    };
                    b1.push_back(temp);
                    t1.shoot_time=clock();
                }
            }
        }
    }

    if(t2.isLive==1){
        if(clock()-t2.shoot_time>=300){
            if(GetAsyncKeyState('M')&0x8000){

                if(b2.size()<8){
                    Bullet temp={
                        t2.x,
                        t2.y,
                        t2.angle+90,
                        10000,
                    };
                    b2.push_back(temp);
                    t2.shoot_time=clock();
                }
            }
        }
    }
}


void Bullets::Bullet_move(){
    vector<Bullet>::iterator it1 = b1.begin();
    vector<Bullet>::iterator it2 = b2.begin();
    while (it1 != b1.end()||it2!=b2.end()) {
        if (it1 != b1.end() && it1->time <= 0) {
            it1 = b1.erase(it1);
            continue;
        }
        if (it2 != b2.end() && it2->time <= 0) {
            it2 = b2.erase(it2);
            continue;
        }

        if (it1 != b1.end()) {
            it1->x = 5 * cos(it1->angle / 180 * pai) + it1->x;
            it1->y = 5 * sin(it1->angle / 180 * pai) + it1->y;
            it1->time = it1->time - 20;
            it1++;

        }
        if (it2 != b2.end()) {
            it2->x = 5 * cos(it2->angle / 180 * pai) + it2->x;
            it2->y = 5 * sin(it2->angle / 180 * pai) + it2->y;
            it2->time = it2->time - 20;
            it2++;
        }

    }
}


void Bullets::paintEvent(QPaintEvent *event){
    Q_UNUSED(event);

    vector<Bullet>::iterator it1 = b1.begin();
    vector<Bullet>::iterator it2 = b2.begin();
    QPainter painter(this);
    QPointF center;
    while (it1 != b1.end() ) {
        center=QPointF(it1->x,it1->y);
        painter.setBrush(Qt::black);
        painter.drawEllipse(center,5,5);
        it1++;

    }

    while (it2 != b2.end()) {
        center=QPointF(it2->x,it2->y);
        painter.setBrush(Qt::black);
        painter.drawEllipse(center,5,5);
        it2++;
    }
}

void Bullets::Bullet_to_wall(vector<Bullet> &b){

    vector<Bullet>::iterator it1 = b.begin();
    while (it1 != b.end()) {
        if (it1 != b.end()) {
            vector<date_map>::iterator it = maps.begin();
            while (it != maps.end()) {
                if ((it1->x >= it->x1 && it1->x <= it->x2) && it1->y < it->y1) {
                    if ((-it1->y + it->y1) <= 5) {
                        it1->angle = -it1->angle;
                        return;
                    }
                }
                if ((it1->x >= it->x1 && it1->x <= it->x2) && it1->y > it->y2) {
                    if ((it1->y - it->y2) <= 5) {
                        it1->angle = -it1->angle;
                        return;
                    }
                }
                if (it1->x < it->x1 && it1->y >= it->y1 && it1->y <= it->y2) {
                    if (it->x1 - it1->x < 5) {
                        it1->angle = 180 - it1->angle;
                        return;
                    }
                }
                if (it1->x > it->x2 && it1->y >= it->y1 && it1->y <= it->y2) {
                    if (-it->x2 + it1->x < 5) {
                        it1->angle = 180 - it1->angle;
                        return;
                    }
                }
                it++;
            }
        }
        if (it1 != b.end()) {
            it1++;
        }
    }
}

bool Bullets::Bullet_to_tank(vector<Bullet> &b,tank &t){
    auto it = b.begin();
    double k1, k2;
    int temp_max_x = 0, temp_min_x = 1100;
    int temp_max_y = 0, temp_min_y = 1100;
    if ((int)(t.angle) % 90 == 0) {

        if (temp_max_x < t.body_xy.lt.x) {
            temp_max_x = t.body_xy.lt.x;
        }
        if (temp_max_x < t.body_xy.rt.x) {
            temp_max_x = t.body_xy.rt.x;
        }
        if (temp_max_x < t.body_xy.rb.x) {
            temp_max_x = t.body_xy.rb.x;
        }
        if (temp_max_x < t.body_xy.lb.x) {
            temp_max_x = t.body_xy.lb.x;
        }

        if (temp_min_x > t.body_xy.lt.x) {
            temp_min_x = t.body_xy.lt.x;
        }
        if (temp_min_x > t.body_xy.rt.x) {
            temp_min_x = t.body_xy.rt.x;
        }
        if (temp_min_x > t.body_xy.rb.x) {
            temp_min_x = t.body_xy.rb.x;
        }
        if (temp_min_x > t.body_xy.lb.x) {
            temp_min_x = t.body_xy.lb.x;
        }

        if (temp_min_y > t.body_xy.lt.y) {
            temp_min_y = t.body_xy.lt.y;
        }
        if (temp_min_y > t.body_xy.rt.y) {
            temp_min_y = t.body_xy.rt.y;
        }
        if (temp_min_y > t.body_xy.rb.y) {
            temp_min_y = t.body_xy.rb.y;
        }
        if (temp_min_y > t.body_xy.lb.y) {
            temp_min_y = t.body_xy.lb.y;
        }

        if (temp_max_y < t.body_xy.lt.y) {
            temp_max_y = t.body_xy.lt.y;
        }
        if (temp_max_y < t.body_xy.rt.y) {
            temp_max_y = t.body_xy.rt.y;
        }
        if (temp_max_y < t.body_xy.rb.y) {
            temp_max_y = t.body_xy.rb.y;
        }
        if (temp_max_y < t.body_xy.lb.y) {
            temp_max_y = t.body_xy.lb.y;
        }
        //cout << temp_max_x << "  " << temp_min_x << "   " << temp_max_y << "   " << temp_min_y<<endl;
    }
    else {
        k1 = tan((t.angle + 90) / 180 * pai);
        k2 = tan((t.angle + 180) / 180 * pai);
    }


    while (it != b.end()) {
        //qDebug()<<it->x<<"  "<<it->y;
        //qDebug()<<temp_max_x<<"  "<<temp_min_x<<"   "<<temp_max_y<<"   "<<temp_min_y;
        if (it->time < 10000-100-40) {
            if ((int)(t.angle) % 90 == 0) {
                if (it->x >= temp_min_x && it->x <= temp_max_x && it->y >= temp_min_y && it->y <= temp_max_y && t.isLive != 0) {
                    t.isLive = 0;
                    it = b.erase(it);
                    //qDebug()<<temp_max_x<<"  "<<temp_min_x<<"   "<<temp_max_y<<"   "<<temp_min_y;
                    //qDebug()<<it->x<<"  "<<it->y<<"   "<<it->time;
                    //qDebug()<<t.body_xy.lt.x<<"   "<<t.body_xy.lt.y<<"   "<<t.body_xy.rb.x<<"   "<<t.body_xy.rb.y;
                    t.Record_Live_Time = clock();
                    //cout << t.Record_Live_Time << endl;
                    continue;
                }
            }
            else {
                double c1, c2, c3, c4;
                k1 = tan(t.angle + 90);
                k2 = tan(t.angle + 180);
                if (k1 < 0) {
                    double temp = k1;
                    k1 = k2;
                    k2 = temp;
                }
                c1 = t.body_xy.rt.y - t.body_xy.rt.x * k1;
                c2 = t.body_xy.rt.y - t.body_xy.rt.x * k2;
                c3 = t.body_xy.lb.y - t.body_xy.lb.x * k1;
                c4 = t.body_xy.lb.y - t.body_xy.lb.x * k2;
                if (c1 < c3) {
                    double temp;
                    temp = c1;
                    c1 = c3;
                    c3 = temp;
                }
                if (c2 < c4) {
                    double temp;
                    temp = c2;
                    c2 = c4;
                    c4 = temp;
                }
                //cout << k1 << "   " << k2 << "   " << c1 << "  " << c3 << "    " << c2 << "    " << c4 << endl;
                double l1 = it->y - k1 * it->x - c1;
                double l2 = it->y - k1 * it->x - c3;
                double l3 = it->y - k2 * it->x - c2;
                double l4 = it->y - k2 * it->x - c4;
                //cout << l1 << "  " << l2 << "  " << l3 << "  " << l4 << endl;
                if (l1 < 0 && l2>0 && l3 < 0 && l4>0&&t.isLive!=0) {
                    t.isLive = 0;
                    it = b.erase(it);
                    t.Record_Live_Time = clock();
                    continue;
                }
            }

        }
        it++;
    }
    return 0;
}

